package de.uni.kassel.campusappver3.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import de.uni.kassel.campusappver3.MainActivity;
import de.uni.kassel.campusappver3.R;
import de.uni.kassel.campusappver3.crawler.ICrawl;
import de.uni.kassel.campusappver3.crawler.UniEventsCrawler;
import de.uni.kassel.campusappver3.model.CrawlerReturnValue;
import de.uni.kassel.campusappver3.model.Resources;

/**
 * This Activity shows events like "uni partys".
 * it uses the UniEventCrawler in order to get the Information.
 * if the user longClicks on a event a Share-dialog'll pop up.
 * 
 * @author ckonze
 *
 */
public class EventActivity extends Activity implements OnItemLongClickListener, ICrawl{

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_events);	
		
		//see in MensaCrawler.java for detailed information about this construction
		if(Resources.getInstance().getEvents() == null){
        	UniEventsCrawler mc = new UniEventsCrawler(this);
        	if (!MainActivity.internetConnection) {
        		createUI();
        		Toast.makeText(getApplicationContext(), "Keine Internetverbindung.", Toast.LENGTH_SHORT).show();
        		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBarEvents);
        		progressBar.setVisibility(View.INVISIBLE);
        	} else {
        		mc.fetch();
        	}
        } else {
        	createUI();
        }
		
		
	}

	/**
	 * fills the listview with crawled data. the data has to be saved in the
	 * Resources singelton as Events
	 */
	private void createUI() {
		
		//if no internet connection, dont create any UI elements
		if (!MainActivity.internetConnection) {
			return;
		}
		
		CrawlerReturnValue events = Resources.getInstance().getEvents();
		ListView lv = (ListView) findViewById(R.id.list);
		LinkedList<String> values = new LinkedList<String>();
		LinkedList<String> eventKeys = new LinkedList<String>(events.getKeys());
		Collections.sort(eventKeys);
		for (String key : eventKeys) {
			ArrayList<String> event = Resources.getInstance().getEvents()
					.getValue(key);

			values.add("" + event.get(0) + "\n\nOrt: " + event.get(3)
					+ "\nAm: " + event.get(1) + "\tum: " + event.get(2));

		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, values);
		
		lv.setAdapter(adapter);
		lv.setOnItemLongClickListener((OnItemLongClickListener) this);
		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBarEvents);
		progressBar.setVisibility(View.INVISIBLE);
	}


	@Override
	public void onCrawlFinish(CrawlerReturnValue crv) {
		Toast.makeText(this.getApplicationContext(),
				"Klicke lang auf ein Event um es zu Teilen!", Toast.LENGTH_LONG ).show();
		Resources.getInstance().setEvents(crv);
		createUI();		
	}

	/**
	 * open a share dialog if the user clicks long on a item
	 */
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		String key = "" + arg2;
		shareIntent.setType("text/plain");
		ArrayList<String> event = Resources.getInstance().getEvents().getValue(key);
		
		String shareString = "Uni Event: "+ event.get(0) 
				+ "\n\nOrt: "+ event.get(3)
				+ "\nAm: " + event.get(1) + "\tum: " + event.get(2);
		
		shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
		startActivity(Intent.createChooser(shareIntent, "Teilen..."));
		return false;
	}

	
}